home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: owl3.office-workstations-ltd.co.uk!not-for-mail
- From: kenn@office-workstations-ltd.co.uk (Ken Nicolson)
- Subject: Re: How to use assert( )
- Message-ID: <316b7c3a.2530799@newshost>
- Date: Wed, 10 Apr 1996 09:22:39 GMT
- References: <4kc3k7$dur@orion.cybercom.net> <smryanDpKxLz.39v@netcom.com>
- Organization: Office Workstations Limited
- Reply-To: kenn@owl-uk.co.uk
- X-Newsreader: Forte Agent .99d/32.182
-
- smryan@netcom.com (@#$%!?!) wrote:
-
- > #include <assert.h>
- >
- > assert(some_expression_which_must_be_true);
- >
- >as in
- >
- > assert(f=fopen("name","r"));
- >
- [snip!]
- >
- > #define NDEBUG
- > #include <assert.h>
- >
- >The assert(predicate) is elided.
- >
- That's true, but unfortunately the code in the assert body gets removed
- too, so your fopen call never gets compiled in the example above!
-
- The simple rules to follow is never put useful code in the assert(), and
- never use it as a substitute for error checking, such as in the example
- above, as your code should gracefully handle a file not being opened, not
- chuck the user out with a fatal error message.
-
- I only use assert() to check things I know *should* be true. Say during
- development if I accidentally call one of my own functions with a null
- pointer, I'm doing something wrong at the code level.
-
- Ken
-
-